﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using transportsystem.DTO;
using transportsystem.BUS;

namespace transportsystem
{
    public partial class frmDaiLyLichTrinh : Form
    {
        DataTable dt;
        bool hideControls = true;
        int iIndex = 0;
        bool addNew = false;
        public frmDaiLyLichTrinh()
        {
            InitializeComponent();
        }
        void HideAddNew(bool value)
        {
            if (value == true)
                pnlAddNew.Visible = false;
            else
                pnlAddNew.Visible = true;
        }
        private void frmDaiLyLichTrinh_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        public void LoadData()
        {
            HideAddNew(hideControls);
            dt = DaiLyLichTrinhBUS.layDanhSachDaiLyLichTrinh();
            if (dt != null && dt.Rows.Count > 0)
            {
                dgv.DataSource = dt;
                lblCount.Text = "Tổng số dòng: " + dt.Rows.Count.ToString();
            }
            iIndex = 0;
            SetDataGridTitle();
            SetValueToControls();
            DanhSachLichTrinh();
            DanhSachDaiLy();
        }
        public void SetDataGridTitle()
        {
            string[] a = new string[3] { "Mã lịch trình", "Mã đại lý", "Thời gian qua" };

            for (int i = 0; i < a.Count(); i++)
                dgv.Columns[i].HeaderText = a[i];
        }
        public void SetValueToControls()
        {
            if (dt.Rows.Count > 0)
            {
                cbLichTrinh.SelectedValue = dt.Rows[iIndex]["malichtrinh"].ToString();
                cbDaiLy.SelectedValue = dt.Rows[iIndex]["madaily"].ToString();
                txtThoiGianQua.Text = dt.Rows[iIndex]["thoigianqua"].ToString().Trim();
            }
        }

        private void chkHideControls_CheckedChanged(object sender, EventArgs e)
        {
            hideControls = !hideControls;
            HideAddNew(hideControls);
        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls();
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls();
        }
        public void DanhSachLichTrinh()
        {
            DataTable dtlichtrinh = LichTrinhBUS.layDanhSachLichTrinh();
            cbLichTrinh.DataSource = dtlichtrinh;
        }
        public void DanhSachDaiLy()
        {
            DataTable dtDaiLy = DaiLyBUS.getDaiLyList();
            cbDaiLy.DataSource = dtDaiLy;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            chkHideControls.Checked = true;
            hideControls = !chkHideControls.Checked;
            HideAddNew(hideControls);
            AddNewPrepair();
        }

        private void AddNewPrepair()
        {
            cbLichTrinh.SelectedIndex = 0;
            cbDaiLy.SelectedIndex = 0;
            txtThoiGianQua.Text = "";

        }
        public void Save()
        {
            DaiLyLichTrinh dllt = new DaiLyLichTrinh();
            dllt.Malichtrinh = Convert.ToInt32(cbLichTrinh.SelectedValue);
            dllt.Madaily = Convert.ToInt32(cbDaiLy.SelectedValue);
            dllt.Thoigianqua = Convert.ToDateTime(txtThoiGianQua.Text);

            bool kq = DaiLyLichTrinhBUS.capNhatDaiLyLichTrinh(dllt, 1);
            if (kq == true)
                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Có lỗi xảy ra! Vui lòng thử lại", "Thông báo", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            LoadData();
            addNew = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (addNew == false)
            {
                DaiLyLichTrinh dllt = new DaiLyLichTrinh();
                dllt.Malichtrinh = Convert.ToInt32(cbLichTrinh.SelectedValue);
                dllt.Madaily = Convert.ToInt32(cbDaiLy.SelectedValue);
                DaiLyLichTrinhBUS.xoaDaiLyLichTrinh(dllt);
                LoadData();
            }
        }
    }
}
